//
//  ElectiveViewController.m
//  ZhongWangYuan
//
//  Created by ZJ on 2018/6/2.
//  Copyright © 2018年 ZJ. All rights reserved.
//

#import "ElectiveViewController.h"
#import "LPZWYChooseClassClassGBViewController.h"
#import "LPZWYChooseClassSubjectGBViewController.h"
#import "LPZWYChooseClassRecomendGBViewController.h"
#import "ZJVidoViewController.h"
#import "YBProjectDetailViewController.h"
#import "LPZWYTwoTableViewWithView.h"
#import "LPZWYSearchViewController.h"
@interface ElectiveViewController ()<LPZWYTwoTableViewWithViewDelegate,YBPopupMenuDelegate>
/**    */
@property (strong, nonatomic) GTPageController  *gtPageVc;

/**  搜索视图  */
@property (strong, nonatomic) UIView  *backSearchView;
/**  判断是否点击分类  */
@property (assign, nonatomic) BOOL  isClickButton;
/**    */
@property (strong, nonatomic) LPZWYTwoTableViewWithView  *twoTableView;
/**    */
@property (strong, nonatomic) UITextField  *searchTextField;
/**    */
@property (strong, nonatomic) NSArray  *firstArrayData;
/**    */
@property (strong, nonatomic) NSArray  *firstFirstArrayData;
/**    */
@property (strong, nonatomic) NSString  *fid;
/**  是否是第一次进入  */
@property (assign, nonatomic) NSInteger   isFirst;
/**  时间选择  */
@property (strong, nonatomic) UILabel *timeLabel;
/**    */
@property (strong, nonatomic) NSMutableArray  *timeArray;



@end

@implementation ElectiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.fid = @"0";
    [self getTypeData];
    [self layoutUI];
    self.leftButton.hidden = YES;
    
    [self addSearch];
    for (int i = 0; i < 20; i ++) {
        [self.timeArray addObject:[NSString stringWithFormat:@"%ld",[NSCalendar currentYear] - i]];
    }
    [self.timeArray insertObject:@"全部" atIndex:0];
}

/**  获取分类数据  */
- (void)getTypeData{
    [LPRequestManager request_mystudyallcoursecategoryWithfid:self.fid successBlock:^(id object) {
        NSString *code = [object objectForKey:@"code"];
        if ([code integerValue] == 1) {
            self.firstArrayData = [object objectForKey:@"data"];
            self.fid = [self.firstArrayData[0] objectForKey:@"id"];
            [self getFirstArray];
        }
    } failBlock:^(id object) {
        
    }];
}

- (void)getFirstArray{
    [LPRequestManager request_mystudyallcoursecategoryWithfid:self.fid successBlock:^(id object) {
        NSString *code = [object objectForKey:@"code"];
        if ([code integerValue] == 1) {
            
        self.firstFirstArrayData = [object objectForKey:@"data"];
            
        }
    } failBlock:^(id object) {
        
    }];
}




- (void)addSearch{
    self.isClickButton = false;
    [self.view addSubview:self.backSearchView];
    self.backSearchView.sd_layout
    .topSpaceToView(self.navigView, 0)
    .leftEqualToView(self.view)
    .rightEqualToView(self.view)
    .heightIs(60 * [UIScreen zj_screenHScale]);
    
    UIButton *typeButton = [UIButton new];
    [typeButton setTitle:@"分类" forState:UIControlStateNormal];
    [typeButton setTitleColor:[UIColor textGrayColor] forState:UIControlStateNormal];
    typeButton.titleLabel.font = LYPFONT(13);
    [typeButton setImage:[UIImage imageNamed:@"option"] forState:UIControlStateNormal];
    [typeButton addTarget:self action:@selector(clickTypeButton) forControlEvents:UIControlEventTouchUpInside];
    [self.backSearchView addSubview:typeButton];
    typeButton.sd_layout
    .topSpaceToView(self.backSearchView, 10 * [UIScreen zj_screenHScale])
    .leftSpaceToView(self.backSearchView, 15 * [UIScreen zj_screenWScale])
    .heightIs(35 * [UIScreen zj_screenHScale])
    .widthIs(35 * [UIScreen zj_screenHScale]);
    
    typeButton.titleEdgeInsets = UIEdgeInsetsMake(0, -typeButton.imageView.frame.size.width, -typeButton.imageView.frame.size.height, 0);
    // 由于iOS8中titleLabel的size为0，用上面这样设置有问题，修改一下即可
    typeButton.imageEdgeInsets = UIEdgeInsetsMake(-typeButton.titleLabel.intrinsicContentSize.height, 0, 0, -typeButton.titleLabel.intrinsicContentSize.width);
    
    
    self.timeLabel = [UILabel new];
    self.timeLabel.text = @"全部";
    self.timeLabel.textColor = [UIColor textGrayColor];
    self.timeLabel.font = LYPFONT(15);
    self.timeLabel.layer.borderColor = [UIColor themeGrayColor].CGColor;
    self.timeLabel.layer.borderWidth = 1;
    self.timeLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *taptime = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTimeButton)];
    [self.timeLabel addGestureRecognizer:taptime];
    [self.backSearchView addSubview:self.timeLabel];
    self.timeLabel.sd_layout
    .topSpaceToView(self.backSearchView, 15 * [UIScreen zj_screenHScale])
    .leftSpaceToView(typeButton, 8 * [UIScreen zj_screenWScale])
    .heightIs(26 * [UIScreen zj_screenHScale])
    .widthIs(70 * [UIScreen zj_screenHScale]);
    
    UIImageView *downImageView = [UIImageView new];
    downImageView.image = [UIImage imageNamed:@"jt_down_twoSix"];
    [self.timeLabel addSubview:downImageView];
    downImageView.sd_layout
    .topEqualToView(self.timeLabel)
    .rightEqualToView(self.timeLabel)
    .bottomEqualToView(self.timeLabel)
    .widthIs(26 * [UIScreen zj_screenWScale]);
    
    
    UIView *backView = [UIView new];
    backView.layer.cornerRadius = 16;
    backView.layer.masksToBounds = YES;
    backView.layer.borderWidth = 1;
    backView.layer.borderColor = [UIColor themeGrayColor].CGColor;
    backView.backgroundColor = [UIColor whiteColor];
    [self.backSearchView addSubview:backView];
    backView.sd_layout
    .topSpaceToView(self.backSearchView, 11 * [UIScreen zj_screenHScale])
    .rightSpaceToView(self.backSearchView, 20 * [UIScreen zj_screenWScale])
    .heightIs(33 * [UIScreen zj_screenHScale])
    .leftSpaceToView(self.timeLabel, 8 * [UIScreen zj_screenWScale]);
    
    
    UIImageView *rightImageView = [UIImageView new];
    rightImageView.contentMode = UIViewContentModeRight;
    rightImageView.image = [UIImage imageNamed:@"search"];
    rightImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapLabel)];
    [rightImageView addGestureRecognizer:tap];
    [backView addSubview:rightImageView];
    rightImageView.sd_layout
    .rightSpaceToView(backView, 5)
    .centerYEqualToView(backView)
    .heightIs(30 * [UIScreen zj_screenHScale])
    .widthIs(50 * [UIScreen zj_screenWScale]);
    
    UITextField *searchLabel = [UITextField new];
    searchLabel.textColor = [UIColor textGrayColor];
    searchLabel.tintColor = [UIColor themeColor];
    searchLabel.font = LYPFONT(14);
    searchLabel.placeholder = @"     输入关键字";
    [searchLabel setValue:[NSNumber numberWithInt:20] forKey:@"paddingLeft"];
    [backView addSubview:searchLabel];
    self.searchTextField = searchLabel;
    searchLabel.sd_layout
    .centerYEqualToView(backView)
    .leftEqualToView(backView)
    .heightIs(33 * [UIScreen zj_screenHScale])
    .rightSpaceToView(rightImageView, 5 * [UIScreen zj_screenWScale]);
    
    
    
    TenGrayShadowView *five = [TenGrayShadowView new];
    [self.backSearchView addSubview:five];
    five.sd_layout
    .bottomEqualToView(self.backSearchView)
    .leftEqualToView(self.backSearchView)
    .rightEqualToView(self.backSearchView)
    .heightIs(5 * [UIScreen zj_screenHScale]);
    
    
}

/**  时间的点击事件  */
- (void)clickTimeButton{
    [YBPopupMenu showRelyOnView:self.timeLabel titles:self.timeArray icons:nil menuWidth:80 showCount:8 delegate:self];
}

/**  分类的点击事件  */
- (void)clickTypeButton{
   
    if (self.isClickButton == false) {
        LPZWYTwoTableViewWithView *two = [[LPZWYTwoTableViewWithView alloc] initWithFrame:CGRectMake(0, LP_NavBarHeight + 75 * [UIScreen zj_screenHScale], [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - LP_NavBarHeight - 75 * [UIScreen zj_screenHScale] - LP_TabBarHeight) firstArray:self.firstArrayData firstFirstArray:self.firstFirstArrayData];
        two.delegate = self;
        two.alpha = 0;
        self.twoTableView = two;
        [self.view addSubview:self.twoTableView];
         [self.twoTableView showView];
        self.isClickButton = YES;
        
    }else{
         [self.twoTableView hideView];
        
    }
   
   
}

/**  搜索点击事件  */
- (void)tapLabel{
    if (self.searchTextField.text.length == 0) {
        [YJProgressHUD showSuccess:@"请输入需要搜索的课程" inview:[UIApplication sharedApplication].keyWindow];
        [self.searchTextField becomeFirstResponder];
        return;
    }
    LPZWYSearchViewController *vc = [LPZWYSearchViewController new];
    vc.searchString = self.searchTextField.text;
    vc.firstId = @"";
    vc.secondId = @"";
    vc.timeString = [self.timeLabel.text isEqualToString:@"全部"] ? @"":self.timeLabel.text;
    [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
    
}

#pragma mark - YBPopupMenuDelegate
- (void)ybPopupMenuDidSelectedAtIndex:(NSInteger)index ybPopupMenu:(YBPopupMenu *)ybPopupMenu{
    self.timeLabel.text = self.timeArray[index];
   
}

#pragma mark - LPZWYTwoTableViewWithViewDelegate
- (void)LPZWYTwoTableViewWithView_click_firstId:(NSString *)firstId secondFid:(NSString *)secondFid threeFid:(NSString *)threeFid{
    LPZWYSearchViewController *vc = [LPZWYSearchViewController new];
    vc.searchString = @"";
    vc.firstId = firstId;
    vc.secondId = secondFid;
    vc.threeId = threeFid;
    vc.timeString = [self.timeLabel.text isEqualToString:@"全部"] ? @"":self.timeLabel.text;
    [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
}

- (void)LPZWYTwoTableViewHide{
    self.isClickButton = false;
}

#pragma mark - 界面
- (void)layoutUI{
    
    self.gtPageVc = [self getDefaultController];
    self.gtPageVc.view.frame = CGRectMake(0, LP_NavViewHeight + 60 * [UIScreen zj_screenHScale] , self.view.width, [UIScreen zj_screenHeight] - LP_NavViewHeight - LP_TabBarHeight  - 60 * [UIScreen zj_screenHScale]);
    [self.view addSubview:self.gtPageVc.view];
    [self addChildViewController:self.gtPageVc];
    self.gtPageVc.view.sd_layout
    .leftSpaceToView(self.view , 0)
    .rightSpaceToView(self.view , 0)
    .topSpaceToView(self.backSearchView , 0)
    .bottomSpaceToView(self.view , LP_TabBarHeight);
    
}


#pragma mark - 私有方法
- (GTPageController *)getDefaultController{
    
    LPZWYChooseClassRecomendGBViewController *introduceVC = [LPZWYChooseClassRecomendGBViewController new];
    LPZWYChooseClassSubjectGBViewController * newsVC = [LPZWYChooseClassSubjectGBViewController new];
    LPZWYChooseClassClassGBViewController *orderVC = [LPZWYChooseClassClassGBViewController new];
    
    
    
    NSArray *titles = @[ @"推荐", @"专题" , @"课程"];
    
    NSArray *viewVcs = @[[introduceVC class] , [newsVC class] ,[orderVC class]];
    
    __block NSMutableArray *titlesWidth = [NSMutableArray array];
    [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGFloat width = [NSString sizeOfString:obj font:16 * [UIScreen zj_textFontScale]  width:self.view.width].width + 12 * [UIScreen zj_screenWScale];
        [titlesWidth addObject:@(width * 1.6)];
    }];
    
    GTPageController *pageVC = [[GTPageController alloc] initWithViewControllerClasses:viewVcs andTheirTitles:titles];
    pageVC.pageAnimatable = YES;
    
    pageVC.itemsWidths = titlesWidth;
    pageVC.postNotification = YES;
    pageVC.menuBGColor = [UIColor clearColor];
    pageVC.menuViewStyle = GTMenuViewStyleLine;
    pageVC.progressColor = [UIColor themeColor];
    pageVC.titleColorSelected = [UIColor themeColor];
    pageVC.titleColorNormal = [UIColor textGrayColor];
    pageVC.titleSizeNormal = 16 * [UIScreen zj_textFontScale];
    pageVC.titleSizeSelected = 17 * [UIScreen zj_textFontScale];
    pageVC.menuHeight = 50 * [UIScreen zj_screenHScale];
    return pageVC;
}

- (UIView *)backSearchView{
    if (!_backSearchView) {
        _backSearchView = [[UIView alloc] initWithFrame:CGRectMake(0, LP_NavViewHeight, [UIScreen zj_screenWith], 60 * [UIScreen zj_screenHScale])];
        
    }
    return _backSearchView;
}

- (NSMutableArray *)timeArray{
    if (_timeArray == nil) {
        _timeArray = [NSMutableArray array];
    }
    return _timeArray;
}

@end
